/**
 * Copyright (c) 2018-2028, Joffee.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sailmi.system.feign;

import com.sailmi.api.IUserProxyClient;
import com.sailmi.common.model.auth.AuthUser;
import com.sailmi.common.tool.api.R;
import com.sailmi.system.entity.UcsAccountuser;
import com.sailmi.system.entity.UserInfo;
import com.sailmi.system.service.IUserService;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * 用户服务Feign实现类
 *
 * @author Joffee
 */
@RestController
public class UserProxyFeignController implements IUserProxyClient {
	private static Logger log = LoggerFactory.getLogger(UserProxyFeignController.class);
	@Resource
	private IUserService service;

	@Override
	public R<AuthUser> userInfo(Long userId) {
		UserInfo userInfo = service.userInfo(userId);
		log.info("Now in UserProvider for sailmi................................."+userInfo);
		AuthUser auth =new AuthUser();
		log.info("Now in UserProvider for sailmi................................."+userInfo.toString());
		if(Objects.nonNull(userInfo)){
			auth.setAccount(userInfo.getUser().getAccount());
			auth.setAvatar(userInfo.getUser().getAvatar());
			auth.setUserName(userInfo.getUser().getRealName());
			auth.setRoles(userInfo.getRoles());
			auth.setUserCode(userInfo.getUser().getCode());
			auth.setEnterprise(String.valueOf(userInfo.getUser().getDefaultEnterpriseId()));
			auth.setTenantId(userInfo.getUser().getTenantId());
			auth.setUserId(userInfo.getUser().getId());
			auth.setPassword(userInfo.getUser().getPassword());
			auth.setPermissions(userInfo.getPermissions());
			auth.setPhone(userInfo.getUser().getPhone());
			auth.setNickName(userInfo.getUser().getName());
			log.info("Now in UserProvider for sailmi................................."+ auth);
			return R.data(auth);
		}else{
			return R.fail("user not exists!");
		}

	}


	@Override
	public R<AuthUser> userInfo(String tenantId,String account, String password) {
		UserInfo userInfo = service.userInfo(tenantId,account, password);
		log.info("Now in UserProvider for sailmi................................."+userInfo);
		AuthUser auth =new AuthUser();
		log.info("Now in UserProvider for sailmi................................."+userInfo.toString());
		if(Objects.nonNull(userInfo)){
			auth.setAccount(userInfo.getUser().getAccount());
			auth.setAvatar(userInfo.getUser().getAvatar());
			auth.setUserName(userInfo.getUser().getRealName());
			auth.setRoles(userInfo.getRoles());
			auth.setUserCode(userInfo.getUser().getCode());
			auth.setEnterprise(String.valueOf(userInfo.getUser().getDefaultEnterpriseId()));
			auth.setTenantId(userInfo.getUser().getTenantId());
			auth.setUserId(userInfo.getUser().getId());
			auth.setPassword(userInfo.getUser().getPassword());
			auth.setPermissions(userInfo.getPermissions());
			auth.setPhone(userInfo.getUser().getPhone());
			auth.setNickName(userInfo.getUser().getName());
			log.info("Now in UserProvider for sailmi................................."+ auth);
			return R.data(auth);
		}else{
			return R.fail("user not exists!");
		}
	}

	@Override
	public R<AuthUser> registerUser(AuthUser authUser) {
		UcsAccountuser ucsAccountuser = new UcsAccountuser();
		ucsAccountuser.setId(authUser.getUserId());
		ucsAccountuser.setUsername(authUser.getAccount());
		ucsAccountuser.setUserEmail(authUser.getMail());
		ucsAccountuser.setPassword(authUser.getPassword());
		ucsAccountuser.setRealName(authUser.getUserName());
		ucsAccountuser.setCode(authUser.getUserCode());
		ucsAccountuser.setHeadImgUrl(authUser.getAvatar());
		ucsAccountuser.setUserPhone(authUser.getPhone());
		boolean rets = service.registerUser(ucsAccountuser);
		if(rets){
			return R.data(authUser);
		}else{
			return R.fail("注册失败");
		}
	}

	@Override
	public R<AuthUser> updateUser(AuthUser authUser) {
		return null;
	}

	@Override
	public R<List> getRoles(String userAccount) {
		return null;
	}

	@Override
	public R<List> getRights(String userAccount) {
		return null;
	}
}
